package editor.dxf;

import java.awt.geom.Point2D;

import org.kabeja.dxf.DXFArc;

public class DXFDoor {
	private static final double DELTA = 2; //2px de dif
	private Point2D.Float bisagra; //bisagra
	private Point2D.Float end; //terminacion de la puerta
	private Point2D.Float adyacent; //a donde cierra
	
	public Point2D.Float getAdyacent() {
		return adyacent;
	}

	public void setAdyacent(Point2D.Float adyacent) {
		this.adyacent = adyacent;
	}

	public static boolean isValid(Point2D.Float start, Point2D.Float end){
		
		return  start.distanceSq(end) > DELTA*4;
	}
	
	public DXFDoor(Point2D.Float start, Point2D.Float end){
		this.end = end;
		this.bisagra = start;
	}
	
	public Point2D.Float getBisagra() {
		return bisagra;
	}
	
	public void setBisagra(Point2D.Float start) {
		this.bisagra = start;
	}
	
	public Point2D.Float getEnd() {
		return end;
	}
	public void setEnd(Point2D.Float end) {
		this.end = end;
	}
	
	public boolean isNear(Point2D.Float pto){
		return bisagra.distanceSq(pto.x, pto.y) < DELTA ||
				end.distanceSq(pto.x, pto.y) < DELTA;
	}

	public boolean isEquivalent(Point2D.Float p1, Point2D.Float p2){
		//System.out.println(p1.x +" "+ p1.y + " " +p2.x +" " + p2.y);
		return (bisagra.distanceSq(p1.x, p1.y) < DELTA && end.distanceSq(p2.x, p2.y) < DELTA) ||
			   (bisagra.distanceSq(p2.x, p2.y) < DELTA && end.distanceSq(p1.x, p1.y) < DELTA);
		
	}
	
	public boolean isHorizontal(){
		return Math.abs(bisagra.getY() - adyacent.getY()) < DELTA;
	}

	/*
	 * Me quedo con la linea mas larga
	 */
	public void mergeLine(Point2D.Float start, Point2D.Float end){
		if(this.isEquivalent(start, end)){
				//puedo llegar a calcular el punto medio.. por ahora no
		}
	}
	
	public boolean addArc(DXFArc arco){
		Point2D.Float p1 = new Point2D.Float((float)arco.getStartPoint().getX(), (float)arco.getStartPoint().getY());
		Point2D.Float p2 = new Point2D.Float((float)arco.getEndPoint().getX(), (float)arco.getEndPoint().getY());
		
		//System.out.println(p1 + " " + p2 + " " + end + " " + bisagra);
		if(p1.distanceSq(end) < DELTA){
			adyacent = p2;

			return true;
		}
		if(p1.distanceSq(bisagra) < DELTA){
			p1 = bisagra; //p1 lo uso de auxiliar
			bisagra = end;
			end = p1;
			adyacent = p2;
			return true;
		}
		
		if(p2.distanceSq(end) < DELTA){
			adyacent = p1;

			return true;
		}
		if(p2.distanceSq(bisagra) < DELTA){
			p2 = bisagra; //p1 lo uso de auxiliar
			bisagra = end;
			end = p2;
			adyacent = p1;
			return true;
		}
		
		return false;
		
	}
	

	
	
	
	

}
